package DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DatabaseImplement {

	DatabaseHelper databaseHelper;
	SQLiteDatabase database;
	Context context;
	public DatabaseImplement(Context context)
	{
		databaseHelper=new DatabaseHelper(context, "vanhung.db", null, 10);
		this.context=context;
	}
	void Open()
	{
		database=databaseHelper.getWritableDatabase();
		
		
	}
	void Close()
	{
		if (database.isOpen()) {
			
			database.close();
		}
		
	}
	public long InsertCommand(ContentValues contentValues,String tableName,String nullColumnHack)
	{
		long x=0;
		try {
			
			Open();
			x= database.insert(tableName, nullColumnHack, contentValues);
			
			
		} catch (Exception e) {
			Log.d("loi Insert", e.getMessage());
		 
		}
		finally
		{
			Close();
		}
		return x;
	}
	public int UpdateCommand(String table,String whereclause,String[]vallues,ContentValues contentValues)
	{
		int x=0;
		try {
			Open();
			x= database.update(table, contentValues, whereclause, vallues);
			
		} catch (Exception e) {
			// TODO: handle exception
			Log.d("loi update", e.getMessage());
		}
		finally
		{
			Close();
		}
		return x;
	}
	public int DeleteCommand(String table,String whereclause,String[]vallues)
	{
		int x=0;
		try {
			Open();
			x=database.delete(table, whereclause, vallues);
			
		} catch (Exception e) {
			Log.d("loi delete", e.getMessage());
		}
		finally{
			Close();
		}
		return x;
	}
	public Cursor GetAll(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
	{
		Cursor cursor=null;
		try {
			Open();
			cursor=database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
			int count=cursor.getCount();
			Log.d("sds", "");
			
		} catch (Exception e) {
			Log.d("loi Get All", e.getMessage());
		}
		finally
		{
			Close();
		}
		return cursor;
	}
	
}
